package com.lapelittle.order.mapper.provider;

import com.lapelittle.order.query.SaleOrderQuery;
import org.apache.ibatis.jdbc.SQL;

public class SaleOrderProvider {
    
    public String queryList(SaleOrderQuery query) {
        SQL sql = new SQL();
        sql.SELECT("*").FROM("sale_order");
        
        buildCondition(sql, query);
        
        sql.ORDER_BY("import_time DESC, id ASC");
        
        String sqlStr = sql.toString() + " LIMIT #{offset}, #{pageSize}";
        return sqlStr;
    }
    
    public String queryCount(SaleOrderQuery query) {
        SQL sql = new SQL();
        sql.SELECT("COUNT(*)").FROM("sale_order");
        
        buildCondition(sql, query);
        
        return sql.toString();
    }
    
    private void buildCondition(SQL sql, SaleOrderQuery query) {
        if (query.getKeyword() != null && !query.getKeyword().isEmpty()) {
            sql.WHERE("order_no LIKE CONCAT('%', #{keyword}, '%')");
        }
        
        if (query.getMerchantCode() != null && !query.getMerchantCode().isEmpty()) {
            sql.WHERE("merchant_code = #{merchantCode}");
        }
        
        if (query.getStartTime() != null && !query.getStartTime().isEmpty()) {
            sql.WHERE("DATE(import_time) >= #{startTime}");
        }
        
        if (query.getEndTime() != null && !query.getEndTime().isEmpty()) {
            sql.WHERE("DATE(import_time) <= #{endTime}");
        }
    }
} 